<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
var products_id = '{{.product.Id}}';
var suppliers = '{{.product.Suppliers.Id}}';

var tables = $('#dataTables').DataTable({
    "sAjaxSource" : '{{ urlfor "EntryreturnController.SelectEntry"}}'
    ,"bLengthChange": false
    ,"fnServerData" : function(sSource, aoData, fnCallback) {
        var params = checkQueryTableData(aoData);
        params.products_id = Number(products_id);
        params.suppliers = Number(suppliers);

        $.post(sSource, JSON.stringify(params),function(resp) {
            fnCallback(resp);
        },'json');
    }
    ,order:[]
    ,columns: [
        {"bSortable": false, render:function(data, type, full, meta) {
                return productGroupName(full.Products);
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.sph;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.cyl;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.add;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.model;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.color;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.size;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.function_info;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.film_color;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.genre;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Products.Spec;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.purchase_num;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return '<span class="'+  (full.purchase_num<=full.Products.UpperLimit ? 'am-text-danger' : '') +'">' + full.Products.UpperLimit + '</span>';
            }}
        ,{"bSortable": false, "width":'8%', render:function(data, type, full, meta) {
                return '<input type="number" name="o-purchase" data-sph="'+ full.Snd.sph +'" data-cyl="'+ full.Snd.cyl +'"  data-add="'+ full.Snd.add +'" data-invid="'+ full.Id +'" value="'+ (full.purchase_num<=full.Products.UpperLimit ? (full.Products.UpperLimit-full.purchase_num) : 0 ) +'">';
        }}
    ]
    ,"aLengthMenu":[100, 200, 300, 400]
	,'bLengthChange': true
    ,"serverSide": true
    ,"bPaginate" : true
    ,"bFilter" : false	//是否启动过滤、搜索功能
    ,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
    ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
    ,"drawCallback": function(settings){
    }
});
$('#submit-details').click(function () {
    var _data = {};
    var tr = $('#dataTables').find('tr');

    var invlist = [];
    $.each(tr, function (idx, val) {
        var ipt = tr.find('[name="o-purchase"]');
        var inv_id = Number(ipt.data('invid'));
        var purchase = Number(ipt.val());
        var sph = ipt.data('sph');
        var add = ipt.data('add');
        var cyl = ipt.data('cyl');
        if(purchase > 0) {
            invlist.push({'inv_id':inv_id, 'purchase':purchase});


        }



    });
    if(invlist.length <= 0 ){
        layer.alert("没有需要采购的商品");return false;
    }
    _data.invlist = JSON.stringify(invlist);
    _data.products_id = products_id;
    _data.suppliers = suppliers;

    $.post('{{ urlfor "ErpEntryController.SecuritySave" }}', _data, function (res) {
        if(res.code == 1) {
            layer.alert(res.msg);return false;
        }
        parent.layer.closeAll();
        top.location = '{{ urlfor "ErpEntryController.Index" }}';
    }, 'json');
    return false;
});

</script>